import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Roles from '../components/power/Roles.vue'
import Rights from '../components/power/Rights.vue'
import Cates from '../components/goods/Cates.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home,
    redirect:'/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Cates }
    ]
  }
]


const router = new VueRouter({
  routes
})

// 路由的导航守卫 拦截未登录的访问请求

router.beforeEach((to, from, next) => {
  // 如果请求的是login就放行
  if (to.path == '/login') return next()
  // 如果请求的不是login那么我们需要获取本地token来判断是否登录 
  const localToken = window.sessionStorage.getItem('token')
  if (!localToken) return next('/login')
  next()
})


export default router
